package com.source.common.dao;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.transaction.support.TransactionTemplate;

public class BaseDao {
	private JdbcTemplate jdbcTemplate;
	private TransactionTemplate transactionTemplate;
	private Logger log = Logger.getLogger(this.getClass().getName());
	
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public TransactionTemplate getTransactionTemplate() {
		return transactionTemplate;
	}
	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List queryList(String sql) throws Exception {
		log.info(sql);
		List list = jdbcTemplate.query(sql, new ResultSetExtractor() {
			@Override
			public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
				ResultSetMetaData rsd = rs.getMetaData();
				int num = rsd.getColumnCount();
				List list = new ArrayList();
				while (rs.next()) {
					String[] strs = new String[num];
					for (int i = 0; i < num; i++) {
						strs[i] = rs.getString(i + 1);
					}
					list.add(strs);
				}
				return list;
			}
			
		});
		return list;
	}
	
	
	
}
